package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ymatou.iapi.livebuyer.parameter.QueryFootprintBean;
import com.ymatou.iapi.livebuyer.service.QueryFootprintCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.livevideo.parameter.RecLiveNotifyBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*买家最近浏览的主播模块
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryFootprint{
	private static QueryFootprintBean queryfootprintBean;
	private static QueryFootprintCall queryfootprintCall;
	private static LiveRoomWapper liveRoomWapper;
	private static int templivePhase=0;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买家最近浏览的主播模块");
	}

	@Before
	public void caseUp() {
		queryfootprintBean=new QueryFootprintBean();
		queryfootprintCall=new QueryFootprintCall();
		liveRoomWapper=new LiveRoomWapper();
		templivePhase=0;
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryfootprintCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer uid) {
		List<Map> fplist = liveRoomWapper.selectLivefootprintByUid(uid);
		JsonArray datas = queryfootprintCall.getJsonObject().get("data").getAsJsonArray();
		//最多查出9个 这里有个问题 开发程序查最后20个计算 如果有过多的无效直播会少
//		Logger.verifyEquals(fplist.size()>9?9:fplist.size(), datas.size(), "验证size");
		//TODO顺序变化
		Logger.debug(fplist);
//		Logger.verifyEquals(fplist.get(0).get("SellerId"),datas.get(0).getAsJsonObject().get("sellerId").getAsInt(),"验证查出的第一个数据一致");
		
		datas.forEach(data->{
			int liveId=data.getAsJsonObject().get("liveId").getAsInt();
			LiveRoom room = liveRoomWapper.selectLiveRoomById(liveId);
			Logger.verifyEquals(room.getUserId(), data.getAsJsonObject().get("sellerId").getAsInt(), "验证sellerId");
			int livestatus=data.getAsJsonObject().get("liveStatus").getAsInt();
			Logger.verifyEquals(room.getLiveStatus(), livestatus, "验证liveStatus");
			int livePhase=0;
			if(livestatus==0||livestatus==1){
				livePhase=10;
			}else if(livestatus==2||livestatus==3){
				livePhase=20;
			}else{
				livePhase=30;
			}
			if(templivePhase==0){
				templivePhase=livePhase;
			}else{
				if(livePhase==20){
					Logger.verifyEquals(20, templivePhase, "验证templivePhase排序直播开始");
				}else if(livePhase==10){
					Logger.verifyNotEquals(30,templivePhase,"验证templivePhase在预告状态前一个不会是回播");
				}
				if(templivePhase==30){
					Logger.verifyNotEquals(10,livePhase,"验证templivePhase回播当前不可能是直播或预告10");
					Logger.verifyNotEquals(20,livePhase,"验证templivePhase回播当前不可能是直播或预告20");
				}
				templivePhase=livePhase;
			}
			Logger.verifyEquals(livePhase, data.getAsJsonObject().get("livePhase").getAsInt(), "验证livePhase");
		});
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryFootprint_001() {
		Logger.start(true,"买家最近浏览的主播模块通过,存量数据");
		try {
			queryfootprintBean.setUserId(1);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();
			Logger.verifyEquals("200", queryfootprintCall.getString("code"), "验证code");
			verifyDB(queryfootprintBean.getUserId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_002() {
		Logger.start(true,"不输入uid失败");
		try {
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();			
			Logger.verifyEquals("100", queryfootprintCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_003() {
		Logger.start(true,"买家没有浏览记录,返回空");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			queryfootprintBean.setUserId(uid);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();			
			Logger.verifyEquals("200", queryfootprintCall.getString("code"), "验证code");
			Logger.verifyEquals("[]", queryfootprintCall.getString("data"), "验证data无数据");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_004() {
		Logger.start(true,"买家最近浏览的主播模块通过,进行中的直播新数据");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			String token=uc.getAccessToken();
			
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			LiveRoomCallService.enterGroup(livedto.getId(), uid);
			Thread.sleep(2000);
			queryfootprintBean.setUserId(uid);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();			
			Logger.verifyEquals("200", queryfootprintCall.getString("code"), "验证code");
			verifyDB(queryfootprintBean.getUserId());
			Logger.verifyEquals(livedto.getId(),queryfootprintCall.getJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("liveId").getAsInt(),"验证期望的第一个显示直播是正在直播live");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_005() {
		Logger.start(true,"买家最近浏览的主播模块通过,预告直播新数据");
		try {
			Logger.comment("创建用户");
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			Logger.comment("创建直播1,回播");
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			
			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(livedto.getId(), 100);
			LiveRoomCallService.LiveNotify(recivenotifyBean);
			Logger.comment("创建直播2,预报");
			CreateSellerLiveBean initcreatebean2 = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto2 = LiveRoomCallService.CreateSellerLive(initcreatebean2);
			
			Logger.comment("用户进入直播");
			LiveRoomCallService.enterGroup(livedto.getId(), uid);
			LiveRoomCallService.enterGroup(livedto2.getId(), uid);
			Thread.sleep(2000);
			queryfootprintBean.setUserId(uid);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();			
			verifyDB(queryfootprintBean.getUserId());
			Logger.verifyEquals(livedto2.getId(),queryfootprintCall.getJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("liveId").getAsInt(),"验证期望的第一个显示直播是预告live");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_006() {
		Logger.start(true,"买家最近浏览的主播模块通过,回播直播新数据");
		try {
			Logger.comment("创建用户");
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			Logger.comment("创建直播1,回播");
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			
			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(livedto.getId(), 100);
			LiveRoomCallService.LiveNotify(recivenotifyBean);
			
			Logger.comment("用户进入直播");
			LiveRoomCallService.enterGroup(livedto.getId(), uid);
			Thread.sleep(2000);
			queryfootprintBean.setUserId(uid);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();			
			verifyDB(queryfootprintBean.getUserId());
			Logger.verifyEquals(livedto.getId(),queryfootprintCall.getJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("liveId").getAsInt(),"验证期望的第一个显示直播是回播live");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_007() {
		Logger.start(true,"买家最近浏览的主播模块,不会露出没有回播地址的回播");
		try {
			Logger.comment("创建用户");
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			Logger.comment("创建直播");
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			LiveRoomCallService.AutoCloseLiveRoomByUserId(livedto.getUserId());
			
			Logger.comment("用户进入直播");
			LiveRoomCallService.enterGroup(livedto.getId(), uid);
			Thread.sleep(2000);
			queryfootprintBean.setUserId(uid);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();
			if(queryfootprintCall.getJsonObject().get("data").getAsJsonArray().size()>0){
				Logger.verifyNotEquals(livedto.getId(),queryfootprintCall.getJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("liveId").getAsInt(),"验证不会露出没有回播地址的回播");
			}
			//如果没有则肯定是不露出
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_008() {
		Logger.start(true,"买家最近浏览的主播模块通过,进行中的直播新数据-查询最近1天");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			String token=uc.getAccessToken();
			
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			LiveRoomCallService.enterGroup(livedto.getId(), uid);
			Thread.sleep(2000);
			queryfootprintBean.setFootprintDays(1);
			queryfootprintBean.setUserId(uid);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();			
			Logger.verifyEquals("200", queryfootprintCall.getString("code"), "验证code");
			verifyDB(queryfootprintBean.getUserId());
			Logger.verifyEquals(livedto.getId(),queryfootprintCall.getJsonObject().get("data").getAsJsonArray().get(0).getAsJsonObject().get("liveId").getAsInt(),"验证期望的第一个显示直播是正在直播live");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//new LiveRoomWapper().updateSystemNotifyCreateTime(liveid, YMTDateUtil.getDateBeforeOrNextSecond(-31*60));
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryFootprint_009() {
		Logger.start(true,"买家最近浏览的主播模块通过,进行中的直播新数据-查询最近1天没有足迹信息-空返回");
		try {
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer uid = uc.getUserid();
			String token=uc.getAccessToken();
			
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
			
			LiveRoomCallService.SellerLiveOnline(livedto.getId(), livedto.getUserId());
			LiveRoomCallService.enterGroup(livedto.getId(), uid);
			//修改足迹时间1天前
			new LiveRoomWapper().updateLivefootprintUpdateTimeByUid(uid, YMTDateUtil.getDateBeforeOrNextSecond(-31*60*60));
			Thread.sleep(2000);
			queryfootprintBean.setFootprintDays(1);
			queryfootprintBean.setUserId(uid);
			queryfootprintCall.setData(queryfootprintBean);
			queryfootprintCall.callService();			
			Logger.verifyEquals("200", queryfootprintCall.getString("code"), "验证code");
			Logger.verifyEquals(0,queryfootprintCall.getJsonObject().get("data").getAsJsonArray().size(),"验证data");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}